package gates;

import mathtools.Complex;
import mathtools.Matrix;
import register.Register;

/**
 * Matrix representation of a iI gate.
 * This implements Gate interface, so consult this for public methods.
 * The constructor for this gate is protected, so generating such an object is left
 * for the factory class <code>Factory</code>
 * @author Richard Inskip, Jan Zaucha
 *
 */
public class ComplexIdentityMatrix extends MatrixGate{

	/** Complex identity matrix i*I */
	private Matrix ComplexIdentity;

	/** Store the target qubit the gate acts on*/	
	private int target;

	/**
	 * protected constructor to instantiate this object
	 * @param target A flag to apply singly or to all the Qubits.
	 */
	protected ComplexIdentityMatrix(int target){
		ComplexIdentity = Matrix.IdentityMatrix(2).multiplyMatrix(new Complex(0,1));
		this.target = target;
	}

	/**
	 *  Apply Method to apply the gate to a register.
	 */
	public void apply(Register r) {
		int numQubits = r.getNumQbits();
		ComplexIdentity = ComplexIdentity.tensorProduct(Matrix.IdentityMatrix((int)Math.pow(2,(numQubits-1))));

		if(target==-1){
			ComplexIdentity.powerMatrix(numQubits);
			r.update(ComplexIdentity.multiplyMatrix(r));
		}
		else{
			r.update(ComplexIdentity.multiplyMatrix(r));
		}

	}


	/** Return the name of this gate
	 * @return name
	 */
	public String getName() {
		return "Complex Identity";
	}


}
